/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.in;

import wtkx.co.ArrayList;
import wtkx.co.List;

import wtkx.ui.Manifest;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;


/**
 * Manifest class that serves as data source for a clipboard or drag/drop
 * operation.
 *
 * @author gbrown
 */
public class LocalManifestAdapter
    implements Transferable 
{
    public final static java.lang.Class ClassDataFlavor = DataFlavor.class;


    private LocalManifest localManifest;

    private ArrayList<DataFlavor> transferDataFlavors = new ArrayList<DataFlavor>();

    public LocalManifestAdapter(LocalManifest localManifest) {
        this.localManifest = localManifest;

        if (localManifest.containsText()) {
            transferDataFlavors.add(DataFlavor.stringFlavor);
        }

        if (localManifest.containsImage()) {
            transferDataFlavors.add(DataFlavor.imageFlavor);
        }

        if (localManifest.containsFileList()) {
            transferDataFlavors.add(DataFlavor.javaFileListFlavor);
        }

        if (localManifest.containsURL()) {
            transferDataFlavors.add(new DataFlavor(URL.class, URL.class.getName()));
        }
    }


    public Object getTransferData(DataFlavor dataFlavor)
        throws UnsupportedFlavorException {
        Object transferData = null;

        int index = transferDataFlavors.indexOf(dataFlavor);
        if (index == -1) {
            throw new UnsupportedFlavorException(dataFlavor);
        }

        if (dataFlavor.equals(DataFlavor.stringFlavor)) {
            transferData = localManifest.getText();
        } else if (dataFlavor.equals(DataFlavor.imageFlavor)) {
            transferData = localManifest.getImage();
        } else if (dataFlavor.equals(DataFlavor.javaFileListFlavor)) {
            transferData = localManifest.getFileList();
        } else if (dataFlavor.getRepresentationClass() == URL.class) {
            transferData = localManifest.getURL();
        } else if (dataFlavor.isRepresentationClassByteBuffer()) {
            // TODO
            /*
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                Object value = localManifest.getValue(key);

                try {
                    serializer.writeObject(value, byteArrayOutputStream);
                    byteArrayOutputStream.close();
                    ByteBuffer byteBuffer = ByteBuffer.wrap(byteArrayOutputStream.toByteArray());

                } catch(Exception exception) {
                    System.err.println(exception);
                }
             */
        }

        return transferData;
    }
    public DataFlavor[] getTransferDataFlavors() {
        return transferDataFlavors.toArray(ClassDataFlavor);
    }
    public boolean isDataFlavorSupported(DataFlavor dataFlavor) {
        return (transferDataFlavors.indexOf(dataFlavor) != -1);
    }
}
